package com.withoutwords.ui;

import com.withoutwords.R;
import com.withoutwords.engine.BitmapCache;
import com.withoutwords.engine.Common;
import com.withoutwords.engine.Game;
import com.withoutwords.engine.GameController;
import com.withoutwords.engine.Round;
import com.withoutwords.engine.settings.Settings;
import com.withoutwords.engine.social.ISocialNetworkController;
import com.withoutwords.engine.social.SocialEngine;
import com.withoutwords.engine.social.SocialNetwork;
import com.withoutwords.engine.social.SocialUser;

import com.withoutwords.ui.custom.MyCustomTextView;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class RoundEndActivity extends BaseActivity implements OnClickListener {
	SocialUser winner;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.roundend);

		ImageButton btn = (ImageButton) findViewById(R.id.btnShareScores);
		btn.setOnClickListener(this);
		/*
		 * ImageButton btnCon = (ImageButton) findViewById(R.id.btnContinue);
		 * btnCon.setOnClickListener(this); btn = (ImageButton)
		 * findViewById(R.id.btnMain); btn.setOnClickListener(this);
		 */
		notificationId = Common.roundEndedNotificatoin;
		Game game = GameController.getInstance().getCurrentGame();

		winner = (SocialUser) getIntent().getSerializableExtra(Common.winner);
		if (winner != null) {
			TextView tv = (TextView) findViewById(R.id.txtWinnerName);
			if (tv != null && winner != null) {
				tv.setText(winner.getName());
			}

			tv = (TextView) findViewById(R.id.txtMediaName);
			if (tv != null) {
				tv.setText(game.getCurrentRound().getMediaName());
			}

			soundResId = R.raw.sound_round_won;
			playSound(soundResId);
		}

	}

	String getSharingString(String winnerSocialId) {
		/*
		 * if the winner is me , new round button is visible and share string is
		 * " I won the game with X,Y and Z .I guessed the film XXXX correctly "
		 * if I am the owner ,new round button is invisible and share string is
		 * " I played the film XXXX and the winner is Y " else if I am the owner
		 * ,new round button is invisible and share string is
		 * " I played with X,Y ,Z to guess the film XXXX "
		 */
		Game game = GameController.getInstance().getCurrentGame();

		Round round = game.getCurrentRound();
		String sharingString = "";
		String mySocialId = Settings.getInstance().getMyInfo().getSocialUser()
				.getSocialID();

		String ownerName = game.getGameOwner().getName();
		if (ownerName == null) {
			ownerName = getResources().getString(R.string.default_friend_name);
		}
		if (winnerSocialId.equalsIgnoreCase(mySocialId)) {

			sharingString = getResources().getString(R.string.wow)
					+ " "+
					getResources().getStringArray(R.array.winner_share_me)[0]
					+ " "+ownerName
					+" "+ getResources().getStringArray(R.array.winner_share_me)[1]
					+ " "+round.getMediaName()
					+ " "+getResources().getString(R.string.smiley);

			/*
			 * sharingString = "WOW !!! I am the winner in " + ownerName +
			 * "'s Act N' Guess game , I guessed " + round.getMediaName() +
			 * " :) ";
			 */

		} else if (mySocialId.equalsIgnoreCase(game.getGameOwner()
				.getSocialID())) {

			sharingString = getResources().getString(R.string.wow)
					+ " "+winner.getName()
					+ " "+getResources().getStringArray(R.array.winner_share_owner)[0]
					+ " "+winner.getName()
					+ " "+getResources().getStringArray(R.array.winner_share_owner)[1]
					+ " "+round.getMediaName()
					+ " "+getResources().getString(R.string.smiley);
			/*
			 * sharingString = "WOW !!! " + winner.getName() +
			 * " is the winner in my Act N' Guess game ," + winner.getName() +
			 * " guessed " + round.getMediaName() + " :) ";
			 */

		} else {

			/*
			 * sharingString = "WOW !!! " + winner.getName() +
			 * " is the winner in " + ownerName + "'s Act N' Guess game ," +
			 * winner.getName() + " guessed " + round.getMediaName() + " :) ";
			 */
			sharingString = getResources().getString(R.string.wow)
					+ " "+winner.getName()
					+ " "+getResources()
							.getStringArray(R.array.winner_share_player)[0]
					+ " "+ownerName
					+ " "+getResources()
							.getStringArray(R.array.winner_share_player)[1]
					+ " "+winner.getName()
					+ " "+getResources()
							.getStringArray(R.array.winner_share_player)[2]
					+ " "+round.getMediaName()
					+ " "+getResources().getString(R.string.smiley);

		}

		return sharingString;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnShareScores: {
			ISocialNetworkController fbController = SocialEngine
					.getSocialEngine().getSocialNetworkController(
							SocialNetwork.Facebook);
			if (winner != null) {
				fbController
						.share(this, getSharingString(winner.getSocialID()));
			}
			break;
		}
		/*
		 * case R.id.btnMain: { GameController.getInstance().endGame(); break; }
		 * case R.id.btnContinue: {
		 * GameController.getInstance().continueNewRound(); break; }
		 */
		}

	}
@Override
public void finish() {
	Game game = GameController.getInstance().getCurrentGame();
	if(game != null)
		GameController.getInstance().leaveGame(game.getGameId());
	super.finish();
}
	@Override
	protected void releaseBitmaps() {
		// TODO Auto-generated method stub
		super.releaseBitmaps();

		BitmapCache.getCacheInstance().deleteDrawable(R.drawable.winner);
	}

	@Override
	protected void setBitmaps() {
		// TODO Auto-generated method stub
		super.setBitmaps();
		setLayoutBitmap(R.id.lyRoundWinner, R.drawable.winner);

	}

	@Override
	protected void unsetBitmaps() {
		// TODO Auto-generated method stub
		super.unsetBitmaps();
		unsetLayoutBitmaps(R.id.lyRoundInfo);

	}

}
